<?php
class Model_Customer{

    public static function getAllCustomerIds()
    {
        $cache = Core_Global::getCache();
        $listIds = $cache->get('Customer:ListIds');

        if (empty($listIds)) {
            $arrData = Business_Customer::getAllId();
            $listIds = array();

            foreach($arrData as $data)
            {

                $listIds[] = $data['CustomerId'];
            }

            $cache->set('Customer:ListIds', $listIds, 604800);
        }

        return $listIds;
    }
    public static function getAllIds($listIds)
    {

        try
        {
            $cache = Core_Global::getCache();
            $cacheKeys = array();

            foreach($listIds as $id)
            {
                $cacheKeys[] = 'Customer:id:' . $id;
            }

            $result = $cache->getMulti($cacheKeys);

            //Miss cache
            if ($result === false || count($result) != count($cacheKeys))
            {
                if ($result === false) $result = array();

                $missKeys = array_diff(array_keys(array_flip($cacheKeys)), array_keys($result));

                array_walk($missKeys, function(&$item){
                    $item = explode(':', $item);
                    $item = $item[count($item) - 1];
                });

                //Get result from database
                $missResult = Business_Customer::getAllIds(implode(',', $missKeys));

                foreach($missResult as $location)
                {
                    $cache->set('Customer:id:' . $location['CustomerID'], $location, 604800);
                }

                $result = array_merge($missResult,$result);
            }
            return $result;
        }
        catch(Zend_Exception $e)
        {
            var_dump($e->getMessage());
        }
    }
    public static function getAll($params)
    {
        $listId = self::getAllCustomerIds();

        $temp = array_splice($listId, $params['start'], $params['end']);

        if (empty($temp))
        {
            return null;
        }

        return self::getAllIds($temp);
    }

	
	public static function create($params){
		
		$result = Business_Customer::create($params);
		if($result){
			$cache = Core_Global::getCache();
			$cache->delete('Customer:ListIds');
			
		}
		return $result;
	}
	
	public static function update($params){
		$result = Business_Customer::update($params);
		if($result){
			$cache = Core_Global::getCache();
			$cache->delete('Customer:id:'.$params['CustomerId']);
		}
		return $result;
	}

    public static  function getAllCustomers(){
        $db = Core_Global::getDatabase();
		$stmt = $db->prepare('CALL getAllCustomers()');
        $stmt->execute();
        $result = $stmt->fetchAll();
        return $result;
    }

    public static function delete($customerId)
    {
        $result = Business_Customer::delete($customerId);
        if ($result) {
            $cache = Core_Global::getCache();
            $cache->delete('Customer:id:' . $customerId);
            $cache->delete('Customer:ListIds');
        }
        return $result;
    }
	

}